package com.ebt.m.proposal_v2.mvp.presenter;

import android.content.Context;
import android.util.Log;

import com.ebt.m.AppContext;
import com.ebt.m.proposal_v2.dao.ProposalApi;
import com.ebt.m.proposal_v2.dao.request.ParamBrandProducts;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseBrand;
import com.ebt.m.proposal_v2.dao.response.ResponseBrandProduct;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.mvp.base.BasePresenter;
import com.ebt.m.proposal_v2.mvp.contract.ProposalProductsContract;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.ResponseError;
import com.ebt.m.proposal_v2.retrofit.ResponsePage;
import com.ebt.m.proposal_v2.utils.ArrayUtils;
import com.ebt.m.proposal_v2.utils.DataUtils;

import java.util.List;

/**
 * @author archer.qi
 *         Created on 2017/6/14.
 */
public class ProposalProductsPresenter extends BasePresenter<ProposalProductsContract.View> implements ProposalProductsContract.Presenter {

    private List<ResponseMain> mAllProductsForCache;
    private int pageIndex = 1; //分页从第一页开始

    public ProposalProductsPresenter(Context context, ProposalProductsContract.View view) {
        super(context, view);
    }

    @Override
    public void start() {
        if (getCompanyType() == MainProductPresenter.COMPANY_TYPE_INS) {
            /* 保险公司： 全部产品*/
            if (mAllProductsForCache == null) {
                mView.showProgress();
                getCompanyBrand();
            } else {
//                mView.bindData(mAllProductsForCache);
            }
        } else {
            mView.showEmpty(false, false, false);
            mView.showProgress();
            getProductsByBrand(generateParams(false, null));
        }
    }

    @Override
    public int getCompanyType() {
        return AppContext.getCurrentUser().getCompanyInfo().getCategory();
    }

    @Override
    public ParamBrandProducts generateParams(boolean isInsCompany, ResponseBrand brand) {
        ParamGetRisks mArg = mView.getArgument();
        ParamBrandProducts param = new ParamBrandProducts();
        param.age = mArg == null ? "30" : mArg.age;
        param.sex = mArg == null ? 1 : mArg.sex;
        param.profession = mArg == null ? 1 : mArg.profession;
        param.birthday = mArg == null ? null : mArg.birthday;
        param.page = pageIndex;

        if (isInsCompany && brand != null) {
            param.brandId = brand.brandId;
            param.brandShortName = brand.brandShortName;
        } else {
            if (mView.getBrandSelect() != null) {
                param.brandId = mView.getBrandSelect().brandId;
                param.brandShortName = mView.getBrandSelect().brandShortName;
            } else {
                return null;
            }
        }
        return param;
    }

    @Override
    public void getProductsByBrand(ParamBrandProducts param) {
        if (param == null) {
            // 如果品牌为空，则加载常用产品
            getCommonProducts(mView.getArgument());
            return;
        }
        ProposalApi.getInstance(mContext).getBrandProductsAndSubmitHistory(param, new OnResponseListener<List<ResponseBrandProduct>>() {
            @Override
            public void onSuccess(List<ResponseBrandProduct> body, ResponsePage page) {
                mView.cancelProgress();
                if (ArrayUtils.isEmpty(body)) {
                    mView.showEmpty(true, false, false);
                    return;
                }
                mView.showEmpty(false, false, false);
                if (getCompanyType() == MainProductPresenter.COMPANY_TYPE_INS) {
                    mAllProductsForCache = DataUtils.toResponseMainList(body);
                    mView.bindData(mAllProductsForCache);
                } else {
                    mView.bindData(DataUtils.toResponseMainList(body));
                }
            }

            @Override
            public void onFailure(ResponseError error) {
                mView.showToast(error == null ? "产品获取失败" : error.message);
                mView.cancelProgress();
                if (error != null && "-102".equals(error.code)) {
                    mView.showEmpty(false, true, false);
                } else {
                    mView.showEmpty(false, false, true);
                }
            }
        }, mView);
    }

    @Override
    public void getCompanyBrand() {
        ProposalApi.getInstance(mContext).getBrands(new OnResponseListener<List<ResponseBrand>>() {
            @Override
            public void onSuccess(List<ResponseBrand> body, ResponsePage page) {
                if (body == null) {
                    mView.cancelProgress();
                    mView.setEmptyView(true);
                    return;
                }
                ResponseBrand rb = body.get(0);
                if (rb == null) {
                    mView.cancelProgress();
                    mView.setEmptyView(true);
                    return;
                }
                getProductsByBrand(generateParams(true, rb));
            }

            @Override
            public void onFailure(ResponseError error) {
                mView.cancelProgress();
                mView.setEmptyView(true);
            }
        }, mView);
    }

    public void getCommonProducts(ParamGetRisks params) {
        params.pageSize = "10"; // 常用产品最多为10
        ProposalApi.getInstance(mContext).getMainProducts(params, new OnResponseListener<List<ResponseMain>>() {
            @Override
            public void onSuccess(List<ResponseMain> body, ResponsePage page) {
                mView.cancelProgress();
                if (body == null) {
                    mView.setEmptyView(true);
                    return;
                }
                mView.setEmptyView(false);
                if (getCompanyType() == MainProductPresenter.COMPANY_TYPE_INS) {
                } else {
                    mView.bindData(body);
                }
            }

            @Override
            public void onFailure(ResponseError error) {
                mView.cancelProgress();
                mView.setEmptyView(true);
            }
        }, mView);
    }
}
